#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
    Created on 2010-4-7

    @author: neatchenheng

          本moduls 将会抓取http://taoke.alimama.com/spreader/search_sh_catalog.htm
          页面上的“所有类目”
'''
import urllib
import xml.dom.minidom
import os
import time
from beautifulsoup.BeautifulSoup import BeautifulSoup


class SearchShop(object):
    search_url = "http://taoke.alimama.com/spreader/search_sh_catalog.htm"
    local_path = "/home/chenheng/workspace/python/taokemen/src/resources/xml/"
    shop_file = local_path + "shop_category.xml"
    TIMEFORMAT = "%Y-%m-%d %X"
    
    def __init__(self):
        self.makeLocalPath()
    
    def makeLocalPath(self): 
        if not os.path.exists(self.local_path):
            os.makedirs(self.local_path)
    
    def collectShop(self,soup):
        doc = xml.dom.minidom.Document()
        select_html = soup.find("select",attrs={"id":"selectType"})
        shop_cate = select_html.findAll("option")
        shop_cate.pop(0)
        root = doc.createElement("shopes")
        doc.appendChild(root)
        for cate in shop_cate :
            shop = doc.createElement("shop")
            shop.setAttribute("value", cate["value"])
            shop.appendChild(doc.createTextNode(cate.string.encode("utf-8")))
            root.appendChild(shop)
            print "[" + time.strftime(self.TIMEFORMAT,time.localtime()) + "]: Collect auction(" + shop.toxml('utf-8') +") succeed"
        doc.writexml(open(self.shop_file,"w"),"", "    ", "\n", "utf-8")    
            
    def execute(self):
        print "========================================================================"
        print "==>> 'SearchShop.execute' schedule "
        html = urllib.urlopen(self.search_url)
        content = html.read()
        if not content == None :
            print "[" + time.strftime(self.TIMEFORMAT,time.localtime()) + "]: Read resource(" + self.search_url + ") succeed"
        else :
            print "[" + time.strftime(self.TIMEFORMAT,time.localtime()) + "]: Read resource(" + self.search_url + ") failed"
        soup = BeautifulSoup(content,fromEncoding="gbk")
        self.collectShop(soup)
        print "========================================================================"

if __name__ == '__main__':
    search_shop = SearchShop()
    search_shop.execute()
    pass